%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% 
%
% AUTHORS:  
%				Fernando JosÃ© Cuenca Reyes 
%				JesÃºs Lorenzo FernÃ¡ndez GÃ³mez
% 				TomÃ¡s Pablo GalÃ¡n-GÃ³mez ObregÃ³n
%				Constantino Antonio GarcÃ­a MartÃ­nez
% 				Carlos Javier LÃ³pez TuriÃ©gano
%			
%
% The command to start the game is "play."
%3
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%%%%%%%
%
% 
% Identifica que la posicion sea del tipo adecuado
%
%%%%%%%


knight_Bishop_King(Position) :-
        findall(Piece,isInSquare(Position, Piece, _File, _Rank),L),
        king(King1), king(King2), bishop(Bishop), knight(Knight),
        hasColour(King1, Colour), hasColour(Bishop, Colour), hasColour(Knight, Colour),
        hasOpponentPiece(Bishop, King2),
        member(King1,L),
        eliminaLista(King1,L,L2),
        member(King2,L2),
        eliminaLista(King2,L2,L3),
        member(Bishop,L3),
        eliminaLista(Bishop,L3,L4),
        member(Knight,L4).
%        eliminaLista(Knight,L4,[]).
        
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%Stalemate
evaluateKBKKPosition(Position, -40000, 'avoid Stalemate') :-
		%write('avoid Stalemate'),
        computer(Player),
        bishop(ComputerBishop),
        king(OpponentKing),
        hasOpponentPiece(ComputerBishop, OpponentKing),
        hasPiece(Player, ComputerBishop),
        kingAhogado(Position, OpponentKing),
        write('stalemate'),write(' '),write(-40000),write('\n'),!.  
 
     
%small delatang's triangle
evaluateKBKKPosition(Position, 4700, 'preparing the checkmate') :-
		%write('previous to checkmate'),
        computer(Player),
        bishop(ComputerBishop),
        knight(ComputerKnight),
        king(OpponentKing),
        king(ComputerKing),
        hasOpponentPiece(ComputerBishop, OpponentKing),
        hasOpponentPiece(ComputerKing, OpponentKing),
        hasOpponentPiece(ComputerKnight, OpponentKing),
        hasPiece(Player, ComputerBishop),
        hasPiece(Player,ComputerKing),
        hasPiece(Player,ComputerKnight),
        isInSquare(Position, ComputerKing, I, J),
        isInSquare(Position, ComputerBishop, Ib, Jb),
        isInSquare(Position, OpponentKing, Io, Jo),
        isInSquare(Position, ComputerKnight, Ik, Jk),	
        smallToCheck(Position,OpponentKing,Io,Jo,I,J,Ib,Jb,Ik,Jk),  
        write('previous to checkMate'),write(' '),write('\n'),!. 
        
%small delatang's triangle
evaluateKBKKPosition(Position, Punct, 'small delatang triangle') :-
		%write('small delatang triangle'),
        computer(Player),
        bishop(ComputerBishop),
        knight(ComputerKnight),
        king(OpponentKing),
        king(ComputerKing),
        hasOpponentPiece(ComputerBishop, OpponentKing),
        hasOpponentPiece(ComputerKing, OpponentKing),
        hasOpponentPiece(ComputerKnight, OpponentKing),
        hasPiece(Player, ComputerBishop),
        hasPiece(Player,ComputerKing),
        hasPiece(Player,ComputerKnight),
        isInSquare(Position, ComputerKing, I, J),
        isInSquare(Position, ComputerBishop, Ib, Jb),
        isInSquare(Position, OpponentKing, Io, Jo),
        isInSquare(Position, ComputerKnight, Ik, Jk),
        smallDelatang(Position,OpponentKing,Io,Jo,I,J,Ib,Jb),  
        %manhattanDistance(Ik,Jk,I,J,Minus),
        Punct is (4600), 
        \+((attacksSquare(Position, OpponentKing, Ib, Jb),\+attacksSquare(Position, ComputerKing, Ib, Jb),\+attacksSquare(Position, ComputerKnight, Ib, Jb))),
        write('small delatang triangle '),write(Ik),write(Jk),write(' '),write(Punct),write('\n'),!. 

%middle to small delatang's triangle
evaluateKBKKPosition(Position, Punct, 'middle to small delatang triangle: The bishop is placed in its ideal small square') :-
		%write('middle to small delatang triangle, second step'),
        computer(Player),
        bishop(ComputerBishop),
        king(OpponentKing),
        king(ComputerKing),
        knight(ComputerKnight),
        hasOpponentPiece(ComputerKnight, OpponentKing),
        hasOpponentPiece(ComputerBishop, OpponentKing),
        hasOpponentPiece(ComputerKing, OpponentKing),
        hasPiece(Player, ComputerBishop),
        hasPiece(Player,ComputerKing),
        hasPiece(Player,ComputerKnight),
        isInSquare(Position, ComputerKing, I, J),
        isInSquare(Position, ComputerKnight, Ik, Jk),
        isInSquare(Position, ComputerBishop, Ib, Jb),
        isInSquare(Position, OpponentKing, Io, Jo),
        \+((attacksSquare(Position, OpponentKing, Ib, Jb),\+attacksSquare(Position, ComputerKing, Ib, Jb),\+attacksSquare(Position, ComputerKnight, Ib, Jb))),
        middleToSmallPlus(Position,OpponentKing,Io,Jo,I,J,Ib,Jb,Ik,Jk), 
        kingDistance(Io,Jo,I,J,KingDis),
        Punct is 4210-KingDis,         
        write('middle to small delatang triangle,second step'),write(' '),write(Punct),write('\n'),!. 
        
        

        
%middle to small delatang's triangle
evaluateKBKKPosition(Position, Punct, 'middle to small delatang triangle: The bishop approaches its ideal small square ') :-
		%write('middle to small delatang triangle'),
        computer(Player),
        bishop(ComputerBishop),
        king(OpponentKing),
        king(ComputerKing),
        knight(ComputerKnight),
        hasOpponentPiece(ComputerKnight, OpponentKing),
        hasOpponentPiece(ComputerBishop, OpponentKing),
        hasOpponentPiece(ComputerKing, OpponentKing),
        hasPiece(Player, ComputerBishop),
        hasPiece(Player,ComputerKing),
        hasPiece(Player,ComputerKnight),
        isInSquare(Position, ComputerKing, I, J),
        isInSquare(Position, ComputerKnight, Ik, Jk),
        isInSquare(Position, ComputerBishop, Ib, Jb),
        isInSquare(Position, OpponentKing, Io, Jo),
        \+((attacksSquare(Position, OpponentKing, Ib, Jb),\+attacksSquare(Position, ComputerKing, Ib, Jb),\+attacksSquare(Position, ComputerKnight, Ib, Jb))),
        middleToSmall(Position,OpponentKing,Io,Jo,I,J,Ib,Jb,Ik,Jk), 
        kingDistance(Io,Jo,I,J,KingDis),
        Punct is 4200-KingDis,           
        write('middle to small delatang triangle'),write(' '),write(Punct),write('\n'),!. 
        




%middle delatang's triangle
evaluateKBKKPosition(Position, X, 'middle delatang triangle') :-
		%write('middle delatang triangle'),
        computer(Player),
        bishop(ComputerBishop),
        king(OpponentKing),
        king(ComputerKing),
        knight(ComputerKnight),
        hasOpponentPiece(ComputerKnight, OpponentKing),
        hasOpponentPiece(ComputerBishop, OpponentKing),
        hasOpponentPiece(ComputerKing, OpponentKing),
        hasPiece(Player, ComputerBishop),
        hasPiece(Player,ComputerKing),
        hasPiece(Player,ComputerKnight),
        isInSquare(Position, ComputerKing, I, J),
        isInSquare(Position, ComputerKnight, Ik, Jk),
        isInSquare(Position, ComputerBishop, Ib, Jb),
        isInSquare(Position, OpponentKing, Io, Jo),
        \+((attacksSquare(Position, OpponentKing, Ib, Jb),\+attacksSquare(Position, ComputerKing, Ib, Jb),\+attacksSquare(Position, ComputerKnight, Ib, Jb))),
        middleDelatang(Position,OpponentKing,Io,Jo,I,J,Ib,Jb,Ik,Jk), 
        getClosestCorrectCorner(Position,OpponentKing,Io,Jo,Ib,Jb,Ic,Jc),      
        kingDistance(I,J,Io,Jo,DistKings),
        (minimumDistanceToEdge(I,J,1)-> X is (4000-DistKings+1); X is (4000-DistKings)),
        write(I),write(J),write('middle delatang triangle'),write(' '),write(X),write('\n'),!. 
        
        
        
%middle delatang's triangle
evaluateKBKKPosition(Position, X, 'previous to middle delatang triangle') :-
        computer(Player),
        bishop(ComputerBishop),
        king(OpponentKing),
        king(ComputerKing),
        knight(ComputerKnight),
        hasOpponentPiece(ComputerKnight, OpponentKing),
        hasOpponentPiece(ComputerBishop, OpponentKing),
        hasOpponentPiece(ComputerKing, OpponentKing),
        hasPiece(Player, ComputerBishop),
        hasPiece(Player,ComputerKing),
        hasPiece(Player,ComputerKnight),
        isInSquare(Position, ComputerKing, I, J),
        isInSquare(Position, ComputerKnight, Ik, Jk),
        isInSquare(Position, ComputerBishop, Ib, Jb),
        isInSquare(Position, OpponentKing, Io, Jo),
        \+((attacksSquare(Position, OpponentKing, Ib, Jb),\+attacksSquare(Position, ComputerKing, Ib, Jb),\+attacksSquare(Position, ComputerKnight, Ib, Jb))),
        previousToMiddleDelatang(Position,OpponentKing,Io,Jo,I,J,Ib,Jb,Ik,Jk),
        getClosestCorrectCorner(Position,OpponentKing,Io,Jo,Ib,Jb,Ic,Jc),        
        kingDistance(I, J,Ic,Jc,DistCorrectKing),
        kingDistance(I,J,Io,Jo,DistKings),X is (3000+DistCorrectKing-DistKings),
        write('previous middle delatang triangle'),write(' '),write(X),write('\n'),!. 



%big to middle delatang's triangle
evaluateKBKKPosition(Position,2251, 'big to medium delatang triangle.The bishop is placed in its ideal medium square. Knight is placed one knight-hop ago from its ideal square.') :-
		%write('big to medium delatang triangle step 3'),
        computer(Player),
        bishop(ComputerBishop),
        king(OpponentKing),
        king(ComputerKing),
        knight(ComputerKnight),
        hasOpponentPiece(ComputerKnight, OpponentKing),
        hasOpponentPiece(ComputerBishop, OpponentKing),
        hasOpponentPiece(ComputerKing, OpponentKing),
        hasPiece(Player, ComputerBishop),
        hasPiece(Player,ComputerKing),
        hasPiece(Player,ComputerKnight),
        isInSquare(Position, ComputerKing, I, J),
        isInSquare(Position, ComputerKnight, Ik, Jk),
        isInSquare(Position, ComputerBishop, Ib, Jb),
        isInSquare(Position, OpponentKing, Io, Jo),
        \+((attacksSquare(Position, OpponentKing, Ib, Jb),\+attacksSquare(Position, ComputerKing, Ib, Jb),\+attacksSquare(Position, ComputerKnight, Ib, Jb))),
		%write(Ik),write(Jk),write('\n'),
		bigToMiddlePlusPlus(Position,OpponentKing,Io,Jo,I,J,Ib,Jb,Ik,Jk),
        write(Ib),write(Jb),write(' '),write('big to medium delatang triangle step 3'),write(' '),write(2251),write('\n'),!. 


%big to middle delatang's triangle
evaluateKBKKPosition(Position,Punct, 'big to medium delatang triangle. The bishop is placed in its ideal medium square. Knight is placed in its ideal big square.') :-
		%write('big to medium delatang triangle step 2'),
        computer(Player),
        bishop(ComputerBishop),
        king(OpponentKing),
        king(ComputerKing),
        knight(ComputerKnight),
        hasOpponentPiece(ComputerKnight, OpponentKing),
        hasOpponentPiece(ComputerBishop, OpponentKing),
        hasOpponentPiece(ComputerKing, OpponentKing),
        hasPiece(Player, ComputerBishop),
        hasPiece(Player,ComputerKing),
        hasPiece(Player,ComputerKnight),
        isInSquare(Position, ComputerKing, I, J),
        isInSquare(Position, ComputerKnight, Ik, Jk),
        isInSquare(Position, ComputerBishop, Ib, Jb),
        isInSquare(Position, OpponentKing, Io, Jo),
        \+((attacksSquare(Position, OpponentKing, Ib, Jb),\+attacksSquare(Position, ComputerKing, Ib, Jb),\+attacksSquare(Position, ComputerKnight, Ib, Jb))),
		bigToMiddlePlus(Position,OpponentKing,Io,Jo,I,J,Ib,Jb,Ik,Jk),
		manhattanDistance(Io,Jo,I,J,DistKings),
		kingDistance(Ik,Jk,I,J,DistKnight),Punct is (2201-DistKings-DistKnight),
        write(I),write(J),write(' '),write('big to medium delatang triangle step 2'),write(' '),write(Punct),write('\n'),!. 

%big to middle delatang's triangle
evaluateKBKKPosition(Position,Punct, 'big to medium delatang triangle. The bishop approaches its ideal medium square') :-
		%write('big to medium delatang triangle'),
        computer(Player),
        bishop(ComputerBishop),
        king(OpponentKing),
        king(ComputerKing),
        knight(ComputerKnight),
        hasOpponentPiece(ComputerKnight, OpponentKing),
        hasOpponentPiece(ComputerBishop, OpponentKing),
        hasOpponentPiece(ComputerKing, OpponentKing),
        hasPiece(Player, ComputerBishop),
        hasPiece(Player,ComputerKing),
        hasPiece(Player,ComputerKnight),
        isInSquare(Position, ComputerKing, I, J),
        isInSquare(Position, ComputerKnight, Ik, Jk),
        isInSquare(Position, ComputerBishop, Ib, Jb),
        isInSquare(Position, OpponentKing, Io, Jo),
		bigToMiddle(Position,OpponentKing,Io,Jo,I,J,Ib,Jb,Ik,Jk,Dis),
		\+((attacksSquare(Position, OpponentKing, Ib, Jb),\+attacksSquare(Position, ComputerKing, Ib, Jb),\+attacksSquare(Position, ComputerKnight, Ib, Jb))),
        manhattanDistance(I,J,Io,Jo,DKK),
        kingDistance(I,J,Io,Jo,KingDistance),KingDistance==2,
        Punct is 2200-5*Dis-DKK,
        \+attacksSquare(Position, ComputerBishop, Io, Jo),
        write(Ib),write(Jb),write(' '),write('big to medium delatang triangle'),write(' '),write(Punct),write('\n'),!. 


%big delatang's triangle
evaluateKBKKPosition(Position, Punct, 'big delatang triangle') :-
		%write('big delatang triangle'),
        computer(Player),
        bishop(ComputerBishop),
        king(OpponentKing),
        king(ComputerKing),
        knight(ComputerKnight),
        hasOpponentPiece(ComputerKnight, OpponentKing),
        hasOpponentPiece(ComputerBishop, OpponentKing),
        hasOpponentPiece(ComputerKing, OpponentKing),
        hasPiece(Player, ComputerBishop),
        hasPiece(Player,ComputerKing),
        hasPiece(Player,ComputerKnight),
        isInSquare(Position, ComputerKing, I, J),
        isInSquare(Position, ComputerKnight, Ik, Jk),
        isInSquare(Position, ComputerBishop, Ib, Jb),
        isInSquare(Position, OpponentKing, Io, Jo),
        bigDelatang(Position,OpponentKing,Io,Jo,I,J,Ib,Jb,Ik,Jk),
        \+((attacksSquare(Position, OpponentKing, Ib, Jb),\+attacksSquare(Position, ComputerKing, Ib, Jb),\+attacksSquare(Position, ComputerKnight, Ib, Jb))),
        manhattanDistance(I,J,Io,Jo,Distance), 
        Punct is 2000-Distance,
        write('big delatang triangle'),write(' '),write(Punct),write('\n'),!. 




% We should not be attacked
evaluateKBKKPosition(Position, -3000, 'avoid Attack') :-
		%write('avoid'),
        computer(Player),
        bishop(ComputerBishop),
        knight(ComputerKnight),
        king(ComputerKing),
        king(OpponentKing),
        hasOpponentPiece(ComputerBishop, OpponentKing),
        hasOpponentPiece(ComputerKing, OpponentKing),
        hasOpponentPiece(ComputerKnight, OpponentKing),
        hasPiece(Player, ComputerKing),
        hasPiece(Player, ComputerBishop),
        hasPiece(Player, ComputerKnight),
        isInSquare(Position, ComputerKnight, Ik, Jk),
        isInSquare(Position, ComputerBishop, Ib, Jb),
        isInSquare(Position, ComputerKing, I, J),
        attacksSquare(Position, OpponentKing, Ib, Jb),\+attacksSquare(Position, ComputerKing, Ib, Jb),\+attacksSquare(Position, ComputerKnight, Ib, Jb),
        write('we should not be attacked'),write(' '),write(-3000),write('\n'),!.

% We should not be attacked
evaluateKBKKPosition(Position, -3000, 'avoid Attack') :-
		%write('avoid v2'),
        computer(Player),
        bishop(ComputerBishop),
        knight(ComputerKnight),
        king(ComputerKing),
        king(OpponentKing),
        hasOpponentPiece(ComputerBishop, OpponentKing),
        hasOpponentPiece(ComputerKing, OpponentKing),
        hasOpponentPiece(ComputerKnight, OpponentKing),
        hasPiece(Player, ComputerKing),
        hasPiece(Player, ComputerBishop),
        hasPiece(Player, ComputerKnight),
        isInSquare(Position, ComputerKnight, Ik, Jk),
        isInSquare(Position, ComputerBishop, Ib, Jb),
        isInSquare(Position, ComputerKing, I, J),
        attacksSquare(Position, OpponentKing, Ik, Jk),\+attacksSquare(Position, ComputerKing, Ik, Jk),\+attacksSquare(Position, ComputerBishop, Ik, Jk),
        write('we should not be attacked'),write(' '),write(-3000),write('\n'),!.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
		
%default
evaluateKBKKPosition(Position,0, indiferent):- 
        write('default'),write(' '),write(0),write('\n'),!.

% This rule should be extended to provide explanations to the moves. DEPRECATED
moveKBKKexplanation(Explanation, _Piece, Explanation).

 
